package com.py.od.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.py.od.domain.*;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.py.od.mapper.OdInventoryTransferMapper;
import com.py.od.service.IOdInventoryTransferService;

/**
 * 库存调拨Service业务层处理
 * 
 * @author py
 * @date 2024-12-04
 */
@Service
public class OdInventoryTransferServiceImpl implements IOdInventoryTransferService
{
    @Autowired
    private OdInventoryTransferMapper odInventoryTransferMapper;





    /**
     * 查询库存调拨
     * 
     * @param inventoryTransferId 库存调拨主键
     * @return 库存调拨
     */
    @Override
    public OdInventoryTransfer selectOdInventoryTransferByInventoryTransferId(Long inventoryTransferId)
    {

        OdInventoryTransfer odInventoryTransfer=odInventoryTransferMapper.selectOdInventoryTransferByInventoryTransferId(inventoryTransferId);
        OdTransferDetails odTransferDetails=new OdTransferDetails();
        odTransferDetails.setTransferNumber(odInventoryTransfer.getTransferNumber());
        List<OdTransferDetails> list=odInventoryTransferMapper.selectByNumberList(odTransferDetails);
        odInventoryTransfer.setOdTransferDetailsList(list);
        return odInventoryTransfer;
    }

    /**
     * 查询库存调拨列表
     * 
     * @param odInventoryTransfer 库存调拨
     * @return 库存调拨
     */
    @Override
    public List<OdInventoryTransfer> selectOdInventoryTransferList(OdInventoryTransfer odInventoryTransfer)
    {
        return odInventoryTransferMapper.selectOdInventoryTransferList(odInventoryTransfer);
    }

    @Override
    public int insertOdInventoryTransfer(OdInventoryTransfer odInventoryTransfer) {
        int add=0;
        odInventoryTransfer.setIsDeleted(0);//未删除

        add=odInventoryTransferMapper.insertOdInventoryTransfer(odInventoryTransfer);
        insertOdTransferDetails(odInventoryTransfer);
        System.out.println(add);
        return add;
    }

    /**
     * 新增库存调拨
     * 
     * @param odInventoryTransfer 库存调拨
     * @return 结果
     */
//    @Transactional
//    @Override
//    public int xgaiOdInventoryTransfer(OdInventoryTransfer odInventoryTransfer)
//    {
//        odInventoryTransfer.setCreateTime(DateUtils.getNowDate());
//        int rows = odInventoryTransferMapper.insertOdInventoryTransfer(odInventoryTransfer);
//        insertOdTransferDetails(odInventoryTransfer);
//        return rows;
//    }

    /**
     * 修改库存调拨
     * 
     * @param odInventoryTransfer 库存调拨
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOdInventoryTransfer(OdInventoryTransfer odInventoryTransfer)
    {
        List<OdTransferDetails> list=odInventoryTransfer.getOdTransferDetailsList();
        list.forEach(l->{
            odInventoryTransferMapper.deleteDetailsByCode(l.getTransferNumber());
        });
        insertOdTransferDetails(odInventoryTransfer);
        return odInventoryTransferMapper.updateOdInventoryTransfer(odInventoryTransfer);
    }

    @Transactional
    @Override
    public int updateStatus(OdInventoryTransfer odInventoryTransfer) {

        return odInventoryTransferMapper.updatestatus(odInventoryTransfer);
    }

//    @Override
//    public int updateKuc(OdTransferDetails odTransferDetails) {
//        return odInventoryTransferMapper.updateKuc(odTransferDetails);
//    }

    /**
     * 批量删除库存调拨
     * 
     * @param inventoryTransferId 需要删除的库存调拨主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOdInventoryTransferByInventoryTransferIds(Long[] inventoryTransferId)
    {


        String [] values=new String[inventoryTransferId.length];
        int index=0;
        for (Long l:inventoryTransferId){
           OdInventoryTransfer odInventoryTransfer=odInventoryTransferMapper.selectOdInventoryTransferByInventoryTransferId(l);
            values[index++]= odInventoryTransfer.getTransferNumber();
        }
        System.out.println("values："+values);
        odInventoryTransferMapper.deleteOdTransferDetailsByTransferNumbers(values);
        return odInventoryTransferMapper.deleteOdInventoryTransferByInventoryTransferIds(inventoryTransferId);
    }

    @Override
    public int updateOdTransferDetails(OdTransferDetails odTransferDetails) {
        return odInventoryTransferMapper.updateOdTransferDetails(odTransferDetails);
    }

    /**
     * 删除库存调拨信息
     * 
     * @param inventoryTransferId 库存调拨主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOdInventoryTransferByInventoryTransferId(Long inventoryTransferId)
    {
        OdInventoryTransfer odInventoryTransfer=odInventoryTransferMapper.selectOdInventoryTransferByInventoryTransferId(inventoryTransferId);
        odInventoryTransferMapper.deleteOdTransferDetailsByTransferNumber(odInventoryTransfer.getTransferNumber());
        return odInventoryTransferMapper.deleteOdInventoryTransferByInventoryTransferId(inventoryTransferId);
    }

    @Override
    public SysUser selectBySysUserName(String userName) {
        return odInventoryTransferMapper.selectBySysUserName(userName);
    }

    /**
     * 新增调拨明细信息
     * 
     * @param odInventoryTransfer 库存调拨对象
     */
    public void insertOdTransferDetails(OdInventoryTransfer odInventoryTransfer)
    {
        List<OdTransferDetails> odTransferDetailsList = odInventoryTransfer.getOdTransferDetailsList();
        String  transferNmber = odInventoryTransfer.getTransferNumber();
        if (StringUtils.isNotNull(odTransferDetailsList))
        {
            List<OdTransferDetails> list = new ArrayList<OdTransferDetails>();
            for (OdTransferDetails odTransferDetails : odTransferDetailsList)
            {
                System.out.println("getDlLocationCode:"+odTransferDetails.getDlLocationCode());
                System.out.println("getDcLocationCode:"+odTransferDetails.getDcLocationCode());
                odTransferDetails.setDlLocationCode(odTransferDetails.getDlLocationCode());
                odTransferDetails.setDcLocationCode(odTransferDetails.getDcLocationCode());
                odTransferDetails.setTransferNumber(transferNmber);
                odTransferDetails.setIsDeleted(0);
                odTransferDetails.setTransferAmount(BigDecimal.valueOf(odTransferDetails.getTransferQuantity()).multiply(odTransferDetails.getInboundPrice()));
                list.add(odTransferDetails);
            }
            if (list.size() > 0)
            {
                odInventoryTransferMapper.batchOdTransferDetails(list);
            }
        }
    }



    @Override
    public List<OdTransferDetails> selectByNumberList(OdTransferDetails odTransferDetails) {
        return odInventoryTransferMapper.selectByNumberList(odTransferDetails);
    }

    @Override
    public List<WhInventory> selectWhInventoryByCode(WhInventory whInventory) {
        return odInventoryTransferMapper.selectWhInventoryByCode(whInventory);
    }

    @Override
    public List<BsWhLocation> selectAllBsWhLocation(Integer wid) {
        return odInventoryTransferMapper.selectAllBsWhLocation(wid);
    }

    @Override
    public WhInventory seleWhInventory(WhInventory whInventory) {
        return odInventoryTransferMapper.seleWhInventory(whInventory);
    }

    @Override
    public int WhinaddAll(WhInventory whInventory) {
        return odInventoryTransferMapper.WhlnaddAll(whInventory);
    }

    @Override
    public int DcWhinupdate(WhInventory whInventory) {

        return odInventoryTransferMapper.DcWhlnupdate(whInventory);

    }

    @Override
    public int DlWhinuppdate(WhInventory whInventory) {
        return odInventoryTransferMapper.DlWhlnupdate(whInventory);
    }

    @Override
    public int deleteDetailsByCode(String transferNumber) {
        return odInventoryTransferMapper.deleteDetailsByCode(transferNumber);
    }

    @Override
    public OdWarehouse selectOdWarehouse(int warehouseId) {
        return odInventoryTransferMapper.selectOdWarehouse(warehouseId);
    }
}
